package org.linlinjava.litemall.brand.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.brand.annotation.LoginUser;
import org.linlinjava.litemall.brand.dto.Account;
import org.linlinjava.litemall.brand.dto.BrandQualify;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.bcrypt.BCryptPasswordEncoder;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static org.linlinjava.litemall.brand.util.BrandResponseCode.AUTH_INVALID_ACCOUNT;

@CrossOrigin
@Transactional
@RestController
@RequestMapping("/brand/brand")
@Validated
public class BrandController {
    private final Log log = LogFactory.getLog(BrandController.class);

    @Autowired
    private LitemallBrandService brandService;
    @Autowired
    private LitemallBrandInfoService brandInfoService;
    @Autowired
    private LitemallBrandQualificationService brandQualificationService;
    @Autowired
    private LitemallQualificationService qualificationService;
    @Autowired
    private LitemallServiceService serviceService;
    @Autowired
    private TUserService tUserService;
    @Autowired
    private LitemallCommodityService commodityService;
    @Autowired
    private LitemallBrandAppendService brandAppendService;

    //获取brand表基本数据
    @GetMapping("/read")
    public Object read(@LoginUser Integer userId) {
        Integer brandId = serviceService.getBelong(userId);
        LitemallBrand brand = brandService.findById(brandId);
        return ResponseUtil.ok(brand);
    }
    //更新brand表基本数据
    @PostMapping("/update")
    public Object update(@RequestBody Account account) {
        String password = account.getNewPw();
        String oldPw = account.getOldPw();
        Integer brandId = account.getBrandId();
        LitemallBrand brand = brandService.findById(brandId);

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if (!encoder.matches(oldPw, brand.getPassword())) {
            return ResponseUtil.fail(AUTH_INVALID_ACCOUNT, "账号密码不对");
        }
        if (!StringUtils.isEmpty(password)) {
            BCryptPasswordEncoder encoder1 = new BCryptPasswordEncoder();
            String encodedPassword1 = encoder1.encode(password);
            brand.setPassword(encodedPassword1);
        }
        if (brandService.updateById(brand) == 0) {
            return ResponseUtil.updatedDataFailed();
        }
        return ResponseUtil.ok(brand);
    }
    //更新brand_info表
    @PostMapping(value = "/updatePri")
    public Object updatePri(@RequestParam(name = "jsonStr") String jsonStr){
        //JacksonUtil.parseString(jsonStr,"brandInfo");
        LitemallBrandInfo brandInfo = JacksonUtil.parseObject(jsonStr, "brandInfo", LitemallBrandInfo.class);
        LitemallBrand brand = brandService.findById(brandInfo.getBrandId());
        brand.setRegionId(brandInfo.getRegion());
        brand.setPicUrl(brandInfo.getPicUrl());
        brandInfoService.update(brandInfo);
        brandService.updateById(brand);
        return ResponseUtil.ok();
    }
    //获取brand_info表数据
    @GetMapping(value = "/readPri")
    public Object readPri(@LoginUser Integer id){
        Integer brandId = serviceService.getBelong(id);
        List<LitemallBrandInfo> byBrandId = brandInfoService.findByBrandId(brandId);
        return ResponseUtil.ok(byBrandId.get(0));
    }
    //获取商户资质
    @GetMapping(value = "/readQl")
    public Object readQl(@LoginUser Integer id){
        Integer brandId = serviceService.getBelong(id);
        List<BrandQualify> brandQualifies = new ArrayList<>();
        List<LitemallBrandQualification> brandQualifications = brandQualificationService.findByBrandIdAndHas(brandId);
        for (LitemallBrandQualification brandQualification : brandQualifications) {
            BrandQualify brandQualify = new BrandQualify();
            brandQualify.setBrandQualification(brandQualification);
            LitemallQualification qualification = qualificationService.findById(brandQualification.getQualificationId());
            brandQualify.setQualifyName(qualification.getName());
            brandQualifies.add(brandQualify);      }
        return ResponseUtil.ok(brandQualifies);
    }
    //更新商户资质
    @PostMapping(value = "updateQl")
    public Object updateQl(@RequestParam(name = "jsonStr") String jsonStr){
        LitemallBrandQualification brandQualification = JacksonUtil.parseObject(jsonStr, "Ql", LitemallBrandQualification.class);
        int flag = brandQualificationService.updateById(brandQualification);
        if (flag==0){
            return ResponseUtil.fail(-1,"修改失败");
        }else{
            return  ResponseUtil.ok();
        }
    }

    @PostMapping(value = "addService")
    public Object addService(LitemallService service){
        TUser tUser = new TUser();
        tUser.setUserName(service.getUsername());
        tUser.setAvatar(service.getAvatar());
        tUser.setNickName(service.getUsername());
        tUserService.insert(tUser);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(service.getPassword());
        service.setPassword(encode);
        service.setImId(tUser.getId());
        serviceService.insert(service);
        return ResponseUtil.ok();
    }

    @PostMapping(value = "updateService")
    public Object updateService(LitemallService service){
        TUser tUser = new TUser();
        tUser.setId(service.getImId());
        tUser.setUserName(service.getUsername());
        tUser.setAvatar(service.getAvatar());
        tUser.setNickName(service.getUsername());
        tUserService.update(tUser);
        serviceService.update(service);
        return ResponseUtil.ok();
    }

    @PostMapping(value = "delService")
    public Object delService(int id){
        serviceService.delete(id);
        return ResponseUtil.ok();
    }
    @PostMapping(value = "listService")
    public Object listService(@LoginUser Integer id){
        Integer brandId = serviceService.getBelong(id);
        List<LitemallService> litemallServices = serviceService.selectByBrandId(brandId);
        return ResponseUtil.ok(litemallServices);
    }

}

